import { defineStore } from 'pinia'
import { get } from '@common'
export const useMember = defineStore('member', {
  state: () => ({
    // 账期类型
    settlement_type_list: [
      {
        label: '账期用户',
        value: 1,
        show_date: true
      },
      {
        label: '非账期用户',
        value: 2
      }
    ],
    websiteGroupList:[] // 站群列表
  }),
  actions: {
    // 根据渠道获取列表,渠道:0平台，1供应商，2项目负责人，3客户端
    // value, key，查询值，查询key
    async getUserListByChannel(channel, params) {
      const setParams = (key, mapKey) => {
        if (params[key]) {
          params[mapKey] = params[key]
          delete params[key]
        }
      }
      channel = parseInt(channel)
      let apiUrl = ''
      switch (channel) {
        case 0:
          apiUrl = 'system/user/list'
          break
        case 1:
          apiUrl = 'system/tenant/list'
          setParams('email', 'userEmail')
          setParams('nickName', 'tenantName')
          break
        case 2:
          apiUrl = 'system/project/list'
          setParams('email', 'userEmail')
          setParams('nickName', 'tenantName')
          break
        case 3:
          apiUrl = 'user/custom/list'
          setParams('nickName', 'name')
          break
        default:
          break
      }
      const res = await get(apiUrl, params)
      res.data = res.data.map((e) => {
        return {
          channel,
          email: e.email || e.userEmail,
          nickName: e.nickName || e.tenantName || e.name,
          uid: e.userId || e.id,
          userName: e.userName || e.email
        }
      })
      return res
    },
    async getWebsiteGroupList () {
      const res = await get('system/websiteGroup/list', {
        pageSize: 1000,
        pageNum: 1
      })
      const list = res.data.map((e) => {
        return {
          ...e,
          label: e.name,
          value: e.id
        }
      })
      this.websiteGroupList = list
    }
  }
})
